package com.linln.admin.medic.controller;

import com.linln.admin.medic.domain.MedicineInfo;
import com.linln.admin.medic.service.MedicineInfoService;
import com.linln.admin.medic.validator.MedicineInfoValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wholve
 * @date 2024/03/25
 */
@Controller
@RequestMapping("/medic/")
public class MedicineInfoController {

    @Autowired
    private MedicineInfoService medicineInfoService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("medic::index")
    public String index(Model model, MedicineInfo medicineInfo) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("title", match -> match.contains())
                .withMatcher("property", match -> match.contains())
                .withMatcher("description", match -> match.contains());

        // 获取数据列表
        Example<MedicineInfo> example = Example.of(medicineInfo, matcher);
        Page<MedicineInfo> list = medicineInfoService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/medic//index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("medic::add")
    public String toAdd() {
        return "/medic//add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("medic::edit")
    public String toEdit(@PathVariable("id") MedicineInfo medicineInfo, Model model) {
        model.addAttribute("medicineInfo", medicineInfo);
        return "/medic//add";
    }

    /**
     * 爬取数据
     */
    @GetMapping("/crawl")
    @RequiresPermissions({"medic::add", "medic::edit"})
    @ResponseBody
    public ResultVo crawl(String keyword) {
        // 保存数据
        medicineInfoService.crawlBySearch(keyword, 1);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 爬取数据
     */
    @GetMapping("/crawlByThread")
    @RequiresPermissions({"medic::add", "medic::edit"})
    @ResponseBody
    public ResultVo crawlByThread(@RequestParam(defaultValue = "3000000") Integer no) throws InterruptedException {
        if (no < 3000000) {
            no = 3000000;
        }
        // 保存数据
        medicineInfoService.crawlByThread(no);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 保存添加/修改的数据
     *
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"medic::add", "medic::edit"})
    @ResponseBody
    public ResultVo save(@Validated MedicineInfoValid valid, MedicineInfo medicineInfo) {
        // 复制保留无需修改的数据
        if (medicineInfo.getId() != null) {
            MedicineInfo beMedicineInfo = medicineInfoService.getById(medicineInfo.getId());
            EntityBeanUtil.copyProperties(beMedicineInfo, medicineInfo);
        }

        // 保存数据
        medicineInfoService.save(medicineInfo);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("medic::detail")
    public String toDetail(@PathVariable("id") MedicineInfo medicineInfo, Model model) {
        model.addAttribute("medicineInfo", medicineInfo);
        return "/medic//detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("medic::status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Integer> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (medicineInfoService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}